package cn.edu.zjut.dao;


import cn.edu.zjut.po.Customer;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.List;

/**
 * @author XiaoFeng
 * @version 1.0
 * @Description Create by 2021/10/27 14:22
 */

public class CustomerDAO extends BaseHibernateDAO{
    private Log log = LogFactory.getLog(CustomerDAO.class);

    public List findByHql(String hql) {
        log.debug("finding LoginUser instance by hql");
        try {
            String queryString = hql;
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by hql failed", re);
            throw re;
        }
    }

    public void save(Customer customer) {
        log.debug("saving customer instance");
        try {
            getSession().save(customer);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    public void update(Customer customer) {
        log.debug("update customer instance");
        try {
            getSession().update(customer);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }
    public void delete(Customer customer) {
        log.debug("delete customer instance");
        try {
            getSession().delete(customer);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Object findById(int customerId) {
        log.debug("find a customer by its customerId");
        try{
            return getSession().get(Customer.class,customerId);
        } catch (RuntimeException re) {
            log.error("find a customer", re);
            throw re;
        }
    }
}
